//
//  GroupedListView.swift
//  whale
//
//  Created by topnet on 2022/7/4.
//

import UIKit


class GroupedListView: UITableView {

    private lazy var disposeBag = DisposeBag()
    private var cellId = "CellId"
    private var data: [GroupedListModelProtocol]?

    init() {
        super.init(frame: .zero, style: .grouped)
        backgroundColor = .background
        contentInsetAdjustmentBehavior = .never
        delegate = self
        dataSource = self
    }
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        backgroundColor = .background
        contentInsetAdjustmentBehavior = .never
        delegate = self
        dataSource = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public func configData(data: [GroupedListModelProtocol]?,
                                            cellType: WhaleListCell.Type? = nil,
                                            cellForRow: ((IndexPath, TableCellModelProtocol, WhaleListCell)->())? = nil,
                                            itemSelected: @escaping ((IndexPath, TableCellModelProtocol)->())) {
        
        register(WhaleListCell.classForCoder(), forCellReuseIdentifier: cellId)
        self.cellType = cellType
        self.cellForRow = cellForRow
        self.itemSelected = itemSelected
        self.data = data
        reloadData()
    }
    
    var dataDelegate: GroupedListProtocol?
    var cellType: WhaleListCell.Type?
    var cellForRow: ((IndexPath, TableCellModelProtocol, WhaleListCell)->())?
    var itemSelected: ((IndexPath, TableCellModelProtocol)->())?
}

extension GroupedListView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return self.data?[section].headerV ?? UIView()
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return self.data?[section].footerV ?? UIView()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return self.data?[section].headerHeight ?? self.data?[section].headerV?.bounds.height ?? 0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return self.data?[section].footerHeight ?? self.data?[section].footerV?.bounds.height ?? 0
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.data?[indexPath.section].rows?[indexPath.row].rowHeight
        ?? self.data?[indexPath.section].rowHeight
        ?? 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if let sec = self.data?[indexPath.section], let rows = sec.rows, rows.count > indexPath.row {
            itemSelected?(indexPath, rows[indexPath.row])
        }
    }
}


extension GroupedListView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data?[section].rows?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let sec = self.data?[indexPath.section], let rows = sec.rows, rows.count > indexPath.row {
            if let cellType = sec.cellType ?? cellType {
                var cell = tableView.dequeueReusableCell(withIdentifier: cellType.description()) as? WhaleListCell
                if cell == nil {
                    cell = cellType.init(style: .default, reuseIdentifier: cellType.description())
                }
                cell?.mod = rows[indexPath.row]
                cellForRow?(indexPath, rows[indexPath.row], cell!)
                return cell!
            }
        }
        return UITableViewCell.init(style: .default, reuseIdentifier: cellId)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.data?.count ?? 0
    }
    

}
